package edu.unf.cen6940.service;

import edu.unf.cen6940.dao.UserloginDAO;

import edu.unf.cen6940.domain.Userlogin;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for Userlogin entities
 * 
 */

@Service("UserloginService")
@Transactional
public class UserloginServiceImpl implements UserloginService {

	/**
	 * DAO injected by Spring that manages Userlogin entities
	 * 
	 */
	@Autowired
	private UserloginDAO userloginDAO;

	/**
	 * Instantiates a new UserloginServiceImpl.
	 *
	 */
	public UserloginServiceImpl() {
	}

	/**
	 * Delete an existing Userlogin entity
	 * 
	 */
	@Transactional
	public void deleteUserlogin(Userlogin userlogin) {
		userloginDAO.remove(userlogin);
		userloginDAO.flush();
	}

	/**
	 * Return all Userlogin entity
	 * 
	 */
	@Transactional
	public List<Userlogin> findAllUserlogins(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Userlogin>(userloginDAO.findAllUserlogins(startResult, maxRows));
	}

	/**
	 */
	@Transactional
	public Userlogin findUserloginByPrimaryKey(Integer userLoginId) {
		return userloginDAO.findUserloginByPrimaryKey(userLoginId);
	}

	/**
	 * Return a count of all Userlogin entity
	 * 
	 */
	@Transactional
	public Integer countUserlogins() {
		return ((Long) userloginDAO.createQuerySingleResult("select count(o) from Userlogin o").getSingleResult()).intValue();
	}

	/**
	 * Save an existing Userlogin entity
	 * 
	 */
	@Transactional
	public void saveUserlogin(Userlogin userlogin) {
		Userlogin existingUserlogin = userloginDAO.findUserloginByPrimaryKey(userlogin.getUserLoginId());

		if (existingUserlogin != null) {
			if (existingUserlogin != userlogin) {
				existingUserlogin.setUserLoginId(userlogin.getUserLoginId());
				existingUserlogin.setUserName(userlogin.getUserName());
				existingUserlogin.setPasswordHash(userlogin.getPasswordHash());
			}
			userlogin = userloginDAO.store(existingUserlogin);
		} else {
			userlogin = userloginDAO.store(userlogin);
		}
		userloginDAO.flush();
	}

	/**
	 * Load an existing Userlogin entity
	 * 
	 */
	@Transactional
	public Set<Userlogin> loadUserlogins() {
		return userloginDAO.findAllUserlogins();
	}
}
